Lesson 3 - Statements, expressions and logic

Programs are made up of many lines of code. Each line of code is referred to as a statement. A statement could be simple, like print "hello" or could be complex. A statement is a instruction to the computer to perform a specific task. An expression is a calculation which would be used by a statement in order to produce its result. The code below contains both an expression (the calculation) and a statement.


print (10 + 2) * 2


Statement - A single instruction in source code which will instruct the computer to perform an action such as display or store a value. Statements can be identified by keywords.

Expression - Part of a statement which will produce a useable value. This could be a arithmetic or logic calculation.

Keyword - A reserved word in the programming language which has a specific purpose. For example print, if, while and else are all considered keywords. Variables are not allowed to be named the same as a keyword.

Expression are always run before the statement as the value they produce is needed by the statement. The order that expressions are evaluated can be controlled by brackets. The next code contains a complex expression and a number of statements and to work out what the result is you must know the order in which the expressions will be evaluated.


x = 10
if x > (8 + 4) - 5:
	print "do I print?"


The order of execution is -

  1. x = 10
  2. (8 + 4)
  3. 12 - 5
  4. x > 7
  5. if 10 > 7
  6. end

Logic expressions play a major part in programming. Logic expression will evaluate to true or false only. Normally logic expressions will compare some value against another. You can find more about logic expressions by reading the pages in the related links. The code below shows some logic expressions in action. You can download the file, logicExpressions.py, by following the link.


x = 12
y = 8
print x > y # displays true
print x < y # displays false
print x == y # displays false
print x != y # displays true
print (x > y) == True # displays true!
print (x < y) == True # displays false!


Logic expression - A expression which will evaluate to true or false.

Note - Logic expressions use comparison operators in order to evaluate. This is covered in lesson 4.